#!/usr/bin/env ruby

RAILS_HOME = File.expand_path(File.join(File.dirname(__FILE__),".."))
BDRB_HOME = File.join(RAILS_HOME,"vendor","plugins","backgroundrb")
WORKER_ROOT = File.join(RAILS_HOME,"lib","workers")
WORKER_LOAD_ENV = File.join(RAILS_HOME,"script","load_worker_env")

["server","server/lib","lib","lib/backgroundrb"].each { |x| $LOAD_PATH.unshift(BDRB_HOME + "/#{x}")}
$LOAD_PATH.unshift(WORKER_ROOT)

require "rubygems"
require "yaml"
require "erb"
require "logger"
require "packet"
require "optparse"

require "bdrb_config"
require RAILS_HOME + "/config/boot"
require "active_support"

BackgrounDRb::Config.parse_cmd_options ARGV
BDRB_CONFIG = BackgrounDRb::Config.read_config("#{RAILS_HOME}/config/backgroundrb.yml")

require RAILS_HOME + "/config/environment"
require "bdrb_job_queue"
require "backgroundrb_server"

pid_file = "#{RAILS_HOME}/tmp/pids/backgroundrb_#{BDRB_CONFIG[:backgroundrb][:port]}.pid"
SERVER_LOGGER = "#{RAILS_HOME}/log/backgroundrb_debug_#{BDRB_CONFIG[:backgroundrb][:port]}.log"

case ARGV[0]
when 'start'
  if fork
    sleep(5)
    exit
  else
    op = File.open(pid_file, "w")
    op.write(Process.pid().to_s)
    op.close
    if BDRB_CONFIG[:backgroundrb][:log].nil? or BDRB_CONFIG[:backgroundrb][:log] != 'foreground'
      log_file = File.open(SERVER_LOGGER,"w+")
      [STDIN, STDOUT, STDERR].each {|desc| desc.reopen(log_file)}
    end

    BackgrounDRb::MasterProxy.new()
  end
when 'stop'
  def kill_process arg_pid_file
    pid = nil
    File.open(arg_pid_file, "r") { |pid_handle| pid = pid_handle.gets.strip.chomp.to_i }
    begin
      pgid =  Process.getpgid(pid)
      Process.kill('TERM', pid)
      Process.kill('-TERM', pgid)
      Process.kill('KILL', pid)
    rescue Errno::ESRCH => e
      puts "Deleting pid file"
    rescue
      puts $!
    ensure
      File.delete(arg_pid_file) if File.exists?(arg_pid_file)
    end
  end
  pid_files = Dir["#{RAILS_HOME}/tmp/pids/backgroundrb_*.pid"]
  pid_files.each { |x| kill_process(x) }
else
  BackgrounDRb::MasterProxy.new()
end

